<template>
    <!-- 为了防止图片下面元素在图片加载前后抖动，添加wrapper -->
    <div class="wrapper">
        <!-- 加入v-if是为了解决 轮播图会自动加载最后一张图片的问题 -->
        <swiper :options="swiperOption" v-if="showSwiper">
            <!-- slides -->
            <swiper-slide v-for='item of list' :key='item.id'>
                <img class='swiper-img' :src="item.imgUrl"/>
            </swiper-slide>
            <!-- 通过slot插槽把数据传入到swiper 用于定制 -->
            <div class="swiper-pagination"  slot="pagination"></div>
        </swiper>
    </div>
    
</template>

<script>
export default {
    name: 'HomeSwiper',
    // 用es6简化data的函数写法data : function(){}
    data(){
        return {
            swiperOption:{
                pagination: '.swiper-pagination',
                loop: true
            }
        }
    },
    props: {
        list: Array
    },
    computed: {
        showSwiper () {
            return this.list.length
        }
    }
}
</script>

<style lang='stylus' scoped>
    // 由于有scoped 所有要用三个箭头穿透 以改变轮播图中的小圆点颜色
    .wrapper >>> .swiper-pagination-bullet-active
        background-color #fff
    .wrapper
        overflow: hidden
        // height: 31.25%
        width: 100%
        height: 0
        padding-bottom: 31.25%
        background: #eee
        .swiper-img
            width: 100%
</style>